import { Client } from "./mod.ts";
import { User } from "./user.ts";
import { Repository } from "./repo.ts";
import { Label, Milestone, StateType } from "./issue.ts";

/**
 * PullRequest represents a pull request.
 */
export interface PullRequest {
  // Issue properties
  id: number;
  number: number;
  user: User;
  title: string;
  body: string;
  labels: Label[];
  milestone?: Milestone;
  assignee?: User;
  state: StateType;
  comments: number;

  // PR specific properties
  head_branch: string;
  head_repo: Repository;
  base_branch: string;
  base_repo: Repository;

  html_url: string;

  mergeable?: boolean;
  merged: boolean;
  merged_at?: string;
  merge_commit_sha?: string;
  merged_by?: User;
}

/**
 * PullRequest API endpoints
 */
export class PullRequestAPI {
  constructor(private client: Client) {}

  /**
   * Get a pull request
   */
  async getPullRequest(owner: string, repo: string, index: number): Promise<PullRequest> {
    return await this.client.doRequest<PullRequest>(
      "GET", 
      `/repos/${owner}/${repo}/pulls/${index}`
    );
  }

  /**
   * List pull requests
   */
  async listPullRequests(owner: string, repo: string, state = "open"): Promise<PullRequest[]> {
    return await this.client.doRequest<PullRequest[]>(
      "GET", 
      `/repos/${owner}/${repo}/pulls?state=${state}`
    );
  }

  /**
   * Merge a pull request
   */
  async mergePullRequest(owner: string, repo: string, index: number): Promise<void> {
    await this.client.doRequest<void>(
      "POST", 
      `/repos/${owner}/${repo}/pulls/${index}/merge`, 
      { "Content-Type": "application/json" }
    );
  }

  /**
   * Check if a pull request has been merged
   */
  async isPullRequestMerged(owner: string, repo: string, index: number): Promise<boolean> {
    try {
      await this.client.doRequest<void>(
        "GET", 
        `/repos/${owner}/${repo}/pulls/${index}/merge`
      );
      return true;
    } catch (error) {
      return false;
    }
  }
} 